package com.qumap.map;

import com.google.gwt.maps.client.overlay.HasMarker;
import com.qumap.item.ItemInfo;
import com.qumap.item.ItemOnMap;

import java.util.Collection;
import java.util.HashMap;
import java.util.Set;
import java.util.TreeSet;

/**
 * Created by IntelliJ IDEA.
 * User: Admin
 * Date: 11.06.11
 * Time: 16:56
 * To change this template use File | Settings | File Templates.
 */
public class TypedItemHolder {

    private ItemInfo item_info;

    protected HashMap<ItemOnMap, HasMarker> marker_map = new HashMap<ItemOnMap, HasMarker>();
    private final MarkerIconHolder marker_image_holder;

    private HashMap<Integer, ItemOnMap> loaded_items = new HashMap<Integer, ItemOnMap>();
    private Set<Integer> loaded_items_indexes = new TreeSet<Integer>();

    public TypedItemHolder(ItemInfo item_info) {
        this.item_info = item_info;
        marker_image_holder = new MarkerIconHolder(item_info);
    }

    public String getType() {
        return item_info.getType();
    }

    public void storeNewItem(ItemOnMap item, HasMarker marker) {
        marker_map.put(item, marker);
    }

    public void setImageToItemMarker(ItemOnMap item, HasMarker marker) {
        marker_image_holder.setImageToMarker(item, marker);
    }

    public HasMarker getItemMarker(ItemOnMap item) {
        return marker_map.get(item);
    }

    public HasMarker deleteItem(ItemOnMap item) {
        HasMarker deleted_item_marker = marker_map.remove(item);
        loaded_items.remove(item.getContent().getId());
        loaded_items_indexes.remove(item.getContent().getId());
        return deleted_item_marker;
    }

    protected boolean isItemLoad(ItemOnMap item) {
        return loaded_items_indexes.contains(item.getContent().getId());
    }

    protected HasMarker updateExistedItem(ItemOnMap loaded_item) {
        ItemOnMap existed_item = loaded_items.get(loaded_item.getContent().getId());
        loaded_item.getContent().putTo(existed_item.getContent());
        return marker_map.get(existed_item);
    }

    protected void storeLoadItem(ItemOnMap load_item) {
        loaded_items_indexes.add(load_item.getContent().getId());
        loaded_items.put(load_item.getContent().getId(),load_item);
    }

    public Collection<ItemOnMap> getAllItems() {
        return marker_map.keySet();
    }

}
